package com.mogist.hqc.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.widget.TextView;

import com.ldf.calendar.Utils;
import com.ldf.calendar.component.State;
import com.ldf.calendar.interf.IDayRenderer;
import com.ldf.calendar.model.CalendarDate;
import com.ldf.calendar.view.DayView;
import com.mogist.hqc.R;

import talex.zsw.basecore.util.ColorTool;
import talex.zsw.basecore.util.TimeTool;


/**
 * Created by ldf on 17/6/26.
 */

@SuppressLint("ViewConstructor")
public class CustomDayView extends DayView
{

	private TextView dateTv;
	private TextView marker;
	private View selectedBackground;
	private View todayBackground;
	private final CalendarDate today = new CalendarDate();

	/**
	 * 构造器
	 *
	 * @param context        上下文
	 * @param layoutResource 自定义DayView的layout资源
	 */
	public CustomDayView(Context context, int layoutResource)
	{
		super(context, layoutResource);
		dateTv = (TextView) findViewById(R.id.date);
		marker = (TextView) findViewById(R.id.maker);
		selectedBackground = findViewById(R.id.selected_background);
		todayBackground = findViewById(R.id.today_background);
	}

	@Override public void refreshContent()
	{
		renderToday(day.getDate());
		renderSelect(day.getState());
		renderMarker(day.getDate(), day.getState());
		super.refreshContent();
	}

	private void renderMarker(CalendarDate date, State state)
	{
		String dateStr = TimeTool.changeFormat(date.toString(),"yyyy-M-d","yyyy-MM-dd");
		if(state == State.NEXT_MONTH || state == State.PAST_MONTH)
		{
			 marker.setVisibility(GONE);
			selectedBackground.setVisibility(GONE);
			dateTv.setTextColor(Color.parseColor("#999999"));
		}
		else
		{
			if(Utils.loadMarkData().containsKey(dateStr))
			{// 有金额
				if(state == State.SELECT)
				{
					selectedBackground.setVisibility(VISIBLE);
					dateTv.setTextColor(ColorTool.getColorById(R.color.text_orange));
				}
				else
				{
					dateTv.setTextColor(Color.parseColor("#333333"));
				}
				marker.setVisibility(VISIBLE);
				marker.setText(Utils.loadMarkData().get(dateStr));
			}
			else
			{
				 marker.setVisibility(GONE);
				selectedBackground.setVisibility(GONE);
				dateTv.setTextColor(Color.parseColor("#999999"));
			}
		}
	}

	private void renderSelect(State state)
	{
		if(state == State.SELECT)
		{
			selectedBackground.setVisibility(VISIBLE);
			dateTv.setTextColor(ColorTool.getColorById(R.color.text_orange));
		}
		else if(state == State.NEXT_MONTH || state == State.PAST_MONTH)
		{
			selectedBackground.setVisibility(GONE);
			dateTv.setTextColor(Color.parseColor("#d5d5d5"));
		}
		else
		{
			selectedBackground.setVisibility(GONE);
			dateTv.setTextColor(Color.parseColor("#999999"));
		}
	}

	private void renderToday(CalendarDate date)
	{
		if(date != null)
		{
			if(date.equals(today))
			{
				dateTv.setText("今");
				todayBackground.setVisibility(VISIBLE);
			}
			else
			{
				dateTv.setText(date.day+"");
				todayBackground.setVisibility(GONE);
			}
		}
	}

	@Override public IDayRenderer copy()
	{
		return new CustomDayView(context, layoutResource);
	}
}
